﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClasesLibreria.dao;
using ClasesLibreria.dominio;
using System.Configuration;

public partial class ConsultaFactura : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            gvFacturas.DataSource = DAOFactura.queryFacturas(Database.connectionString(), "", "NroFacturaCliente DESC");
            gvFacturas.DataBind();

            cmdAnular.Enabled = false;
        }
    }
    protected void gvFacturas_SelectedIndexChanged(object sender, EventArgs e)
    {
        GridViewRow row = gvFacturas.SelectedRow;

        Factura factura = new Factura();
        factura = DAOFactura.getFacturaById(int.Parse(row.Cells[0].Text), Database.connectionString());

        Session["factura_consultada"] = factura;

        gvItems.DataSource = DAOFactura.queryDetalles(int.Parse(row.Cells[0].Text), ConfigurationManager.ConnectionStrings["Libreria"].ConnectionString);
        gvItems.DataBind();

        if (factura.FechaAnulacion == null || factura.FechaAnulacion == DateTime.MinValue)
        { cmdAnular.Enabled = true; }
        else
        { cmdAnular.Enabled = false; }
    }
    protected void gvFacturas_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvFacturas.PageIndex = e.NewPageIndex;
       // gvFacturas.DataSource = ConfigurationManager.ConnectionStrings["Libreria"].ConnectionString;
        gvFacturas.DataBind();

    }
    protected void cmdAnular_Click(object sender, EventArgs e)
    {
        Factura fac = new Factura();
        fac = (Factura)Session["factura_consultada"];
        Session["factura_a_anular"] = fac;
        Response.Redirect("AnulacionFactura.aspx");
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        gvFacturas.DataSource = DAOFactura.queryFacturas(Database.connectionString(), txtFiltroFecha.Text, "NroFacturaCliente DESC");
        gvFacturas.DataBind();
    }
    protected void gvFacturas_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (Session["OrderField"] == null)
        {
            Session["OrderField"] = e.SortExpression;
            Session["Order"] = "DESC";
        }
        string strOrder;
        if (Session["OrderField"].ToString() == e.SortExpression.ToString())
        {
            Session["OrderField"] = e.SortExpression;
            strOrder = e.SortExpression;
            if (Session["Order"].ToString() == "ASC")
            {
                strOrder = strOrder + " DESC";
                Session["Order"] = "DESC";
            }
            else
            {
                strOrder = strOrder + " ASC";
                Session["Order"] = "ASC";
            }
        }
        else
        {
            strOrder = e.SortExpression + " ASC";
            Session["OrderField"] = e.SortExpression;
            Session["Order"] = "ASC";
        }
        Session["OrdenGrilla"] = strOrder;
        gvFacturas.DataSource = DAOFactura.queryFacturas(Database.connectionString(), txtFiltroFecha.Text, Session["OrdenGrilla"].ToString());
        gvFacturas.DataBind();
    }
}